<?php
/**
 * Created by IntelliJ IDEA.
 * User: kaireewu
 * Date: 11-10-8
 * Time: 下午2:35
 * To change this template use File | Settings | File Templates.
 */
require_once('inc/common.inc.php');
!defined('IN_SYS') && exit("Access Denied.");
header("content-type:text/html;charset=utf-8");
$pageTitle = "前端开发组  资源库";
$data = array();
$member = array();
$categories = array();
$cid = getgpc('cid', 'G');
$cid = is_numeric($cid) ? $cid : 0;
$avatar = "http://www.gravatar.com/avatar/00000000000000000000000000000000?s=50&d=mm&r=x";
if($db) {
    $sql = "SELECT m.uid as uid,m.uname as uname,m.url as uurl, m.email as email, m.nickname as nickname, d.sid as sid,d.url as url,d.title as title,d.intro as intro,d.posttime as posttime FROM datas AS d INNER JOIN members AS m ON d.uid = m.uid";
    if($cid) {
        $sql .= " WHERE d.cid=?";
    }
    $sql .= " ORDER BY d.uid ASC";
    $query = $db->prepare($sql);
    if($cid){
        $query->execute(array($cid));
    }else{
        $query->execute();
    }
    $data = $query->fetchAll(PDO::FETCH_ASSOC);
    $query = $db->query("SELECT uid, uname, nickname, url as uurl, email FROM members");
    $member = $query->fetchAll(PDO::FETCH_ASSOC);
    $query = $db->query("SELECT cid, cname FROM categories");
    $categories = $query->fetchAll(PDO::FETCH_ASSOC);
    unset($db);
}
?>
<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title><?php echo "$pageTitle - $appName"; ?></title>
    <link rel="stylesheet" href="<?php echo $sitePath?>css/global.css?v=20111209001">
</head>
<body>
<div class="page">
<?php
include_once('header.inc.php');
?>
    <div class="wrap">
        <h1 class="title"><a href="http://webteam.isd.com/doclib/"><?php echo $pageTitle;?></a> </h1>
        <div class="aside">
            <h3>资源分类</h3>
            <ul class="category">
                <li><a href="index.php"<?php if($cid==0){echo " class=\"current\"";}?>>全部资源</a></li>
<?php
        foreach ($categories as $row){
            $class = ($row['cid'] == $cid) ? " class=\"current\"" : '';
            echo sprintf("                <li><a href=\"index.php?cid=%d\"%s>%s</a></li>\n", $row['cid'], $class, $row['cname']);
        }
?>
            </ul>
            <h3>队友</h3>
            <div class="team">
<?php
foreach($member as $m) {
    if ($m['email']) {
        $avatar = "http://www.gravatar.com/avatar/". md5(strtolower(trim($m['email']))) . "?s=50&d=mm&r=x";
    }
    $m['uurl'] = ($m['uurl']) ? $m['uurl'] : '/';
    echo "                <a href=\"{$m['uurl']}\" class=\"avatar\" title=\"{$m['nickname']}\"><img src=\"{$avatar}\" alt=\"头像照片\"><span>{$m['nickname']}</span></a>\n";
}
?>
            </div>

            <h3>最新更新</h3>
            <ul class="update">
<?php
if($data){
    $i = 0;
    while ($i < 5) {
        if($i >= count($data))
            break;
        echo "                <li><a target=\"_blank\"  href=\"{$data[$i]['url']}\" title=\"{$data[$i]['intro']}\">{$data[$i]['title']}</a><span>phosted by {$data[$i]['uname']} on ".date('Y-m-d', $data[$i]['posttime'])."</span></li>\n";
        $i++;
    }
}
?>
            </ul>
        </div>
        <div class="content">
            <div class="add_info" id="addInfo"<?php if(!$logined){ echo' style="display:none;"';}?>>
                <form id="addForm" class="add_form" method="post">
                    <fieldset>
                        <p>
                            <strong class="label">资源标题：</strong>
                            <span class="fields"><label for="addTitle" class="hide-clip">输入标题</label><input id="addTitle" name="addTitle" type="text" class='s-text'></span>
                        </p>
                        <p>
                            <strong class="label">资源网址：</strong>
                            <span class="fields"><label for="addUrl" class="hide-clip">输入网址</label><input id="addUrl" name="addUrl" type="text" class='s-text'></span>
                        </p>
                        <p>
                            <strong class="label">所属分类：</strong>
                            <span class="fields">
                                <label for="addCategory" class="hide-clip">选择所属分类</label>
                                <select id="addCategory" name="addCategory">
<?php
foreach($categories as $c){
    echo sprintf("                                   <option value=\"%d\">%s</option>\n", $c['cid'], $c['cname']);
}
?>
                                </select>
                            </span>
                        </p>
                        <p class="form-textarea">
                            <strong class="label">资源简介：</strong>
                            <span class="fields">
                                <label for="addContent" class="hide-clip">输入简介（可选）</label>
                                <b class="fluid-input"><b class="fluid-input-inner"><textarea id="addContent" name="addContent" class="m-text"></textarea></b></b>
                            </span>
                        </p>
                        <input type="hidden" name="sid" id="hiddenSId" value="0">
                        <input type="submit" class="btn" value="提交">
                        <span class="result-msg"></span>
                    </fieldset>
                </form>
            </div>
<?php
$html = '';
if($data) {
    echo "        <ul class=\"author_list\">\n";
    $group = array();
    foreach($data as $val) {
        $group[$val['uname']][] = $val;
    }
    ksort($group, SORT_STRING);
    foreach($group as $key=>$d) {
        $uinfo = $d[0];
        $uinfo['uurl'] = ($uinfo['uurl']) ? $uinfo['uurl'] : '/';
        if(!empty($uinfo['email'])){
            $avatar = "http://www.gravatar.com/avatar/". md5(strtolower(trim($uinfo['email']))) . "?s=50&d=mm&r=x";
        }
        echo "            <li id=\"author-{$uinfo['uid']}\">\n";
        echo "                <a href=\"{$uinfo['uurl']}\" class=\"avatar\"><img src=\"{$avatar}\" alt=\"头像照片\"><span>{$uinfo['nickname']}</span></a>\n";
        echo "                <div class=\"info-content\">\n";
        echo "                    <ol>\n";
        foreach($d as $item) {
            echo "                        <li>\n";
            echo "                            <a target=\"_blank\"  href=\"{$item['url']}\" title=\"{$item['intro']}\">{$item['title']}</a>\n" ;
            if($item['uid'] == $user['uid']){
                echo "                            <div class=\"btns\"><a href=\"javascript:void(0)\" data-id=\"{$item['sid']}\" class=\"link-edit-item\"><span>修改</span></a>&nbsp;&nbsp;<a href=\"javascript:void(0)\" data-id=\"{$item['sid']}\" class=\"link-del-item\"><span>删除</span></a>\n";
            }
            echo "                        </li>\n";
        }
        echo "                    </ol>\n";
        echo "                </div>\n";
        echo "            </li>\n";
    }
}
    echo "        </ul>\n"
?>
        </div>
    </div>
</div>
<?php
include_once('footer.inc.php');
?>

<?php if($logined) {
echo <<<EOF
<script type="text/javascript">
    var user = {
        "uid":"{$user['uid']}",
        "uname":"{$user['uname']}",
        "email":"{$user['email']}",
        "nickname":"{$user['nickname']}",
        "url":"{$user['url']}"
    };
</script>\n
EOF;
} ?>
<script type="text/javascript" src="<?php echo $sitePath; ?>js/index.js?v=20111209001"></script>
</body>
</html>

